/* Data1.do (STATA)
	Construct asset price data.
	by Ralph Koijen & Motohiro Yogo */

#delimit ;
clear all;
set more off;
set type double;

cap log close;
log using Data1, replace;

/* Define local and global variables */

local Nstep = 1e3;
local Mgap = 1e-9;
local tau = .01;	/* Nelson-Siegel decay factor */

local matST = .25;	/* Maturity of ST debt */
local matLT = 5;	/* Maturity of LT debt */

do global;


/* Load characteristics data */

u year country Counterpart counterpart pbv yieldST yieldLT _Gfx _retIR3M _retMS
	using Data0, clear;

/* Construct zero-coupon yield curve assuming that 
	yieldST is 3-month zero-coupon yield and yieldLT is 10-year par yield */

gen beta0 = 0 if !missing(yieldST) & !missing(yieldLT);
gen beta1 = 0 if !missing(yieldST) & !missing(yieldLT);

qui forval i = 1/`Nstep' {;

	/* Update parameters */

	replace beta0 = yieldST-beta1*(1-exp(-`matST'/`tau'))/(`matST'/`tau');

	gen error = ln((exp(yieldLT)-1)*
		(exp(-1*(beta0+beta1*(1-exp(-1/`tau'))/(1/`tau')))
		+exp(-2*(beta0+beta1*(1-exp(-2/`tau'))/(2/`tau')))
		+exp(-3*(beta0+beta1*(1-exp(-3/`tau'))/(3/`tau')))
		+exp(-4*(beta0+beta1*(1-exp(-4/`tau'))/(4/`tau')))
		+exp(-5*(beta0+beta1*(1-exp(-5/`tau'))/(5/`tau')))
		+exp(-6*(beta0+beta1*(1-exp(-6/`tau'))/(6/`tau')))
		+exp(-7*(beta0+beta1*(1-exp(-7/`tau'))/(7/`tau')))
		+exp(-8*(beta0+beta1*(1-exp(-8/`tau'))/(8/`tau')))
		+exp(-9*(beta0+beta1*(1-exp(-9/`tau'))/(9/`tau')))
		+exp(-10*(beta0+beta1*(1-exp(-10/`tau'))/(10/`tau'))))
		+exp(-10*(beta0+beta1*(1-exp(-10/`tau'))/(10/`tau'))));

	replace beta1 = beta1-error;

	/* Check convergence */

	sum error;

	local gap = max(abs(r(min)),abs(r(max)));

	drop error;

	/* Exit if converged */

	if `gap'<`Mgap' {;
		continue, break;
	};
};

/* Construct zero-coupon yields */

forval i = 1/10 {;
	gen yield`i' = beta0+beta1*(1-exp(-`i'/`tau'))/(`i'/`tau');

	label var yield`i' "`i'-year zero-coupon yield (continuously compounded)";
};

drop yieldLT;

/* Construct future returns */

sort country year;

forval i = 2/10 {;
	by country: gen _ret`i' = `i'*yield`i'-`=`i'-1'*yield`=`i'-1'[_n+1];

	label var _ret`i' "Future: `i'-year zero-coupon return (continuously compounded)";
};

/* Expand to asset type */

expand 3;

bysort year country: gen byte type = _n;

/* Construct prices */

gen maturity = `matST' if type==1;
replace maturity = `matLT' if type==2;

gen yield = yieldST if type==1;
replace yield = yield`matLT' if type==2;

gen mb = exp(-maturity*yield) if type<=2;
replace mb = pbv if type==3;

drop pbv yieldST;

/* Construct returns */

gen _ret = _retIR3M+_Gfx if type==1;
replace _ret = _ret`matLT'+_Gfx if type==2;
replace _ret = _retMS if type==3;

drop _Gfx _retIR3M _retMS;

/* Sample criteria */

keep if inrange(year,$year_min,$year_max);

/* Label variables */

label var type		"Asset type";

label var beta0		"Nelson-Siegel long-run level";
label var beta1		"Nelson-Siegel short-run component";

label var maturity	"Maturity";
label var yield		"Yield (continuously compounded)";
label var mb		"Ratio: Market value to Book value";
label var _ret		"Future: Return (US$, continuously compounded)";

label define type_label
	1 "Short-term debt"
	2 "Long-term debt"
	3 "Equity";

label val type type_label;

/* Save data */

sort year country type;

save Data1, replace;

log close;
